package com.hanxing.kxcharge.module.operations.controller.admin.station;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.CommonResult;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;
import static com.hanxing.kxcharge.framework.common.pojo.CommonResult.success;

import com.hanxing.kxcharge.framework.excel.core.util.ExcelUtils;

import com.hanxing.kxcharge.framework.operatelog.core.annotations.OperateLog;
import static com.hanxing.kxcharge.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hanxing.kxcharge.module.operations.controller.admin.station.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.station.StationDO;
import com.hanxing.kxcharge.module.operations.service.station.StationService;

@Tag(name = "管理后台 - 站点信息")
@RestController
@RequestMapping("/operations/station")
@Validated
public class StationController {

    @Resource
    private StationService stationService;

    @PostMapping("/create")
    @Operation(summary = "创建站点信息")
    @PreAuthorize("@ss.hasPermission('operations:station:create')")
    public CommonResult<Long> createStation(@Valid @RequestBody StationSaveReqVO createReqVO) {
        return success(stationService.createStation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点信息")
    @PreAuthorize("@ss.hasPermission('operations:station:update')")
    public CommonResult<Boolean> updateStation(@Valid @RequestBody StationSaveReqVO updateReqVO) {
        stationService.updateStation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('operations:station:delete')")
    public CommonResult<Boolean> deleteStation(@RequestParam("id") Long id) {
        stationService.deleteStation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('operations:station:query')")
    public CommonResult<StationRespVO> getStation(@RequestParam("id") Long id) {
        StationDO station = stationService.getStation(id);
        return success(BeanUtils.toBean(station, StationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点信息分页")
    @PreAuthorize("@ss.hasPermission('operations:station:query')")
    public CommonResult<PageResult<StationRespVO>> getStationPage(@Valid StationPageReqVO pageReqVO) {
        PageResult<StationDO> pageResult = stationService.getStationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点信息 Excel")
    @PreAuthorize("@ss.hasPermission('operations:station:export')")
    @OperateLog(type = EXPORT)
    public void exportStationExcel(@Valid StationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StationDO> list = stationService.getStationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点信息.xls", "数据", StationRespVO.class,
                        BeanUtils.toBean(list, StationRespVO.class));
    }

}